---@diagnostic disable: lowercase-global, undefined-global, trailing-space

local allgems = {
    ["redgem"] = true,
    ["bluegem"] = true,
    ["yellowgem"] = true,
    ["greengem"] = true,
    ["purplegem"] = true,
    ["orangegem"] = true,
}

---@type data_componentaction[]
local data = {
    -- finiteuse修理
    {
        id = 'ACTION_YMH_REPAIR_FINITEUSE',
        str = STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_REPAIR_FINITEUSE,
        fn = function (act)
            if act.doer ~= nil and act.invobject ~= nil and act.target ~= nil then
                return (function (obj,tar)
                    if tar.prefab and obj.prefab and tar.components.finiteuses then
                        local delta = TUNING.MOD_YUEMIHENG.REPAIR[string.upper(tar.prefab)] and TUNING.MOD_YUEMIHENG.REPAIR[string.upper(tar.prefab)][string.upper(obj.prefab)]
                        if delta then
                            local cur = tar.components.finiteuses:GetPercent()
                            local new = math.min(1,cur + delta)
                            tar.components.finiteuses:SetPercent(new)
                            YMHS:consumeOneItem(obj)
                            -- lol_wp_divine_nofiniteuses
                            if tar:HasTag(tar.prefab..'_nofiniteuses') then
                                tar:RemoveTag(tar.prefab..'_nofiniteuses')
                            end
                            return true
                        end
                    end
                    return false
                end)(act.invobject,act.target)
            end
            return false
        end,
        state = 'give',
        actiondata = {
            mount_valid = true,
            priority = 100,
        },
        type = "USEITEM",
        component = 'inventoryitem',
        testfn = function (inst, doer, target, actions, right)
            if right and doer:HasTag("player") and target.prefab and TUNING.MOD_YUEMIHENG.REPAIR[string.upper(target.prefab)] then
                local canrepair = inst and inst.prefab and TUNING.MOD_YUEMIHENG.REPAIR[string.upper(target.prefab)][string.upper(inst.prefab)]
                if canrepair then
                    return true
                end
            end
            return false
        end,
    },
    -- armor组件 修复
    {
        id = 'ACTION_YMH_ARMOR_REPAIR',
        str = STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_REPAIR_FINITEUSE,
        fn = function (act)
            if act.doer and act.doer:IsValid() and act.invobject and act.invobject:IsValid() and act.target and act.target:IsValid() then
                return (function (obj,tar)
                    local cur_percent = tar.components.armor and tar.components.armor:GetPercent()
                    local repair_percent = obj and obj.prefab and TUNING.MOD_YUEMIHENG.REPAIR_ARMOR[string.upper(tar.prefab)][string.upper(obj.prefab)]
                    if cur_percent and repair_percent then
                        local new_percent = math.min(cur_percent + repair_percent,1)
                        tar.components.armor:SetPercent(new_percent)

                        YMHS:consumeOneItem(obj)

                        if tar:HasTag(tar.prefab..'_nofiniteuses') then
                            tar:RemoveTag(tar.prefab..'_nofiniteuses')
                        end
                        return true
                    end
                    return false
                end)(act.invobject,act.target)
            end
            return false
        end,
        state = 'give',
        actiondata = {
            mount_valid = true,
            priority = 10,
        },
        type = 'USEITEM',
        component = 'inventoryitem',
        testfn = function (inst, doer, target, actions, right)
            if doer:HasTag("player") and target.prefab and TUNING.MOD_YUEMIHENG.REPAIR_ARMOR[string.upper(target.prefab)] then
                local canrepair = inst and inst.prefab and TUNING.MOD_YUEMIHENG.REPAIR_ARMOR[string.upper(target.prefab)][string.upper(inst.prefab)]
                if canrepair then
                    return true
                end
            end
            return false
        end
    },
    -- 吃大理石
    {
        id = 'ACTION_YMH_EAT_MARBLE',
        str = STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_EAT_MARBLE,
        fn = function (act)
            if act.doer and act.invobject then
                return (function (doer,obj)
                    if doer.components.ymh_eat_marble then
                        doer.components.ymh_eat_marble:DoDelta(1)
                        YMHS:consumeOneItem(obj)
                        return true
                    end
                    return false
                end)(act.doer,act.invobject)
            end
            return false
        end,
        state = 'eat',
        actiondata = {
            priority = 10,
            mount_valid = true,
        },
        type = 'INVENTORY',
        component = '_ymh_marble_caneat',
        testfn = function (inst, doer, actions, right)
            return doer:HasTag("player") and inst.prefab and inst.prefab == "marble" and doer.replica.ymh_eat_marble and not doer.replica.ymh_eat_marble:IsMax()
        end,
    },
    -- 快速照顾一个植物,并且一定范围内的植物都会被照顾
    {
        id = 'ACTION_YMH_TEND_FARMPLANT',
        str = STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_TEND_FARMPLANT,
        fn = function (act)
            if act.doer and act.target then
                return (function (doer,tar)
                    local x,_,z = tar:GetPosition():Get()
                    local ents = TheSim:FindEntities(x, 0, z, TUNING.MOD_YUEMIHENG.AVATAR.YUEMIHENG.AOETEND_RADIUS, {"farm_plant"}, {"INLIMBO"})
                    for _,v in pairs(ents) do
                        if v and v:IsValid() then
                            if v.components.farmplanttendable then
                                v.components.farmplanttendable:TendTo(doer)
                            end
                        end
                    end
                    if doer.components.talker ~= nil then
                        doer.sg:AddStateTag("idle") -- allow talker state to take over
                        doer.components.talker:Say(GetString(doer, "ANNOUNCE_TALK_TO_PLANTS"))
                    end
                    return true
                end)(act.doer,act.target)
            end
            return false
        end,
        state = "doshortaction",
        actiondata = {
            priority = 10,
            mount_valid = false,
        },
        type = 'SCENE',
        component = "farmplanttendable",
        testfn = function (inst, doer, actions, right)
            return doer:HasTag("player") and doer.prefab == 'yuemiheng' and inst:HasTag("tendable_farmplant")
        end
    },
    -- 拆绝望石柱子
    {
        id = 'ACTION_YMH_PUNCH_DAYWALKER_PILLAR',
        str = STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_PUNCH_DAYWALKER_PILLAR,
        fn = function (act)
            if act.doer and act.target then
                return (function (tar,doer)
                    if tar.components.workable then
                        tar.components.workable:WorkedBy(doer, 100)
                        return true
                    end
                    return false
                end)(act.target,act.doer)
            end
            return false
        end,
        state = 'dolongaction',
        actiondata = {
            mount_valid = false,
            priority = 10,
        },
        type = 'SCENE',
        component = 'workable',
        testfn = function (inst, doer, actions, right)
            return doer:HasTag("player") and doer.prefab == 'yuemiheng' and inst.prefab == 'daywalker_pillar'
        end
    },
    -- 加入一颗彩虹宝石变成骡的量产机，无耐久
    {
        id = 'ACTION_YMH_SET_TOOL_INFINITEUSE',
        str = STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_SET_TOOL_INFINITEUSE,
        fn = function (act)
            return (function (doer,obj,tar)
                if doer and obj and tar then
                    if tar.components.finiteuses then
                        tar:RemoveComponent('finiteuses')
                    end
                    if not tar:HasTag('ymh_tool_removed_finiteuse') then
                        tar:AddTag('ymh_tool_removed_finiteuse')
                    end
                    if tar.components.named then
                        tar.components.named:SetName(STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_SET_TOOL_INFINITEUSE_RENAME)
                    end
                    YMHS:consumeOneItem(obj)
                    return true
                end
                return false
            end)(act.doer,act.invobject,act.target)
        end,
        state = 'give',
        actiondata = {
            priority = 10,
            mount_valid = false,
        },
        type = "USEITEM",
        component = 'inventoryitem',
        testfn = function (inst, doer, target, actions, right)
            return doer:HasTag("player") and inst.prefab == "opalpreciousgem" and target.prefab == 'yuemiheng_tool' and not target:HasTag('ymh_tool_removed_finiteuse')
        end
    },
    -- 专用武器： “普通的枪”, 可用各色宝石一个升级，无耐久，
    {
        id = 'ACTION_YMH_SPEAR_UPGRADE',
        str = STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_SPEAR_UPGRADE,
        fn = function (act)
            return (function (obj,tar)
                if obj and tar and tar.ymh_spear_upgrade then
                    local obj_prefab = obj.prefab
                    if not tar.ymh_spear_upgrade[obj_prefab] then
                        YMHS:consumeOneItem(obj)
                        tar.ymh_spear_upgrade[obj_prefab] = true

                        local doupgrade = true
                        for _,v in pairs(tar.ymh_spear_upgrade) do
                            if v ~= nil and not v then
                                doupgrade = false
                                break
                            end
                        end

                        if doupgrade then
                            if tar.components.finiteuses then
                                tar:RemoveComponent("finiteuses")
                            end
                            tar.is_ymh_spear_upgrade = true
                            tar:AddTag('is_ymh_spear_upgrade')
                        end
                        
                        return true
                    end
                end
                return false
            end)(act.invobject,act.target)
        end,
        state = "give",
        actiondata = {
            priority = 10,
            mount_valid = false,
        },
        type = "USEITEM",
        component = 'inventoryitem',
        testfn = function (inst, doer, target, actions, right)
            if doer:HasTag("player") and target.prefab == 'yuemiheng_spear' and not target:HasTag('is_ymh_spear_upgrade') then
                if inst.prefab then
                    return allgems[inst.prefab]
                end
            end
            return false
        end
    },
    {
        id = 'ACTION_YMH_STAFF_UPGRADE',
        str = STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_SPEAR_UPGRADE,
        fn = function (act)
            return (function (obj,tar)
                if obj and tar and tar.ymh_staff_upgrade then
                    local obj_prefab = obj.prefab
                    if tar.ymh_staff_upgrade[obj_prefab] and tar.ymh_staff_upgrade[obj_prefab] > 0 then
                        YMHS:consumeOneItem(obj)
                        tar.ymh_staff_upgrade[obj_prefab] = tar.ymh_staff_upgrade[obj_prefab] - 1

                        local doupgrade = true
                        for _,v in pairs(tar.ymh_staff_upgrade) do
                            if v ~= nil and v > 0 then
                                doupgrade = false
                                break
                            end
                        end

                        if doupgrade then
                            if tar.components.finiteuses then
                                tar:RemoveComponent("finiteuses")
                            end
                            tar.is_ymh_staff_upgrade = true
                            tar:AddTag('is_ymh_staff_upgrade')
                        end
                        return true
                    end
                end
                return false
            end)(act.invobject,act.target)
        end,
        state = "give",
        actiondata = {
            priority = 10,
            mount_valid = false,
        },
        type = "USEITEM",
        component = 'inventoryitem',
        testfn = function (inst, doer, target, actions, right)
            if doer:HasTag("player") and target.prefab == 'yuemiheng_staff' and not target:HasTag('is_ymh_staff_upgrade') then
                if inst.prefab then
                    if inst.prefab == "horrorfuel" or inst.prefab == "purebrilliance" then
                        return true
                    end
                end
            end
            return false
        end
    },
    -- 帽子可用两个彩虹宝石升级至无耐久
    {
        id = 'ACTION_YMH_HAT_UPGRADE',
        str = STRINGS.MOD_YUEMIHENG.ACTIONS.ACTION_YMH_SPEAR_UPGRADE,
        fn = function (act)
            return (function (obj,tar)
                if obj and tar then
                    if tar.ymh_hat_gem_num then
                        tar.ymh_hat_gem_num = tar.ymh_hat_gem_num + 1
                    end

                    if tar.ymh_hat_gem_num >= 2 then
                        if not tar:HasTag('ymh_hat_upgraded') then
                            tar:AddTag('ymh_hat_upgraded')
                        end
                        if tar.components.finiteuses then
                            tar:RemoveComponent('finiteuses')
                        end
                    end

                    YMHS:consumeOneItem(obj)

                    return true
                end
                return false
            end)(act.invobject,act.target)
        end,
        state = "give",
        actiondata = {
            priority = 10,
            mount_valid = false,
        },
        type = "USEITEM",
        component = "inventoryitem",
        testfn = function (inst, doer, target, actions, right)
            return doer:HasTag("player") and inst.prefab == "opalpreciousgem" and target.prefab == 'yuemiheng_hat' and not target:HasTag('ymh_hat_upgraded')
        end
    }
}
---@type data_componentaction_change[]
local change = {
    {
        type = 'EQUIPPED',
        component = 'spellcaster',
        testfn = function (old_testfn, inst, doer, target, actions, right,...)
            if doer and doer.prefab and doer.prefab == 'yuemiheng' then
                if right and doer:HasTag('ymh_allow_staff_cast') then
                    table.insert(actions, ACTIONS.CASTSPELL) 
                end
            elseif old_testfn ~= nil then
                old_testfn(inst, doer, actions, right,...)
            end
        end
    }
}

return data,change

